''''''

# Standard
# Related
import lx
# Local
import moopy.al.utility


_layerservice = lx.Service('layerservice')
_layerservice_query = _layerservice.query
_layerservice_select = _layerservice.select

def get_all_vertex_indices():
    ''''''
    _layerservice_select('layer.id', 'selected')

    _layerservice_select('verts', 'all')
    return moopy.al.utility.convert_strings_to_ints(
        _layerservice_query('verts'))

def get_selected_uvs(mesh_layer_index, vmap_index):
    '''Return a list of uvs in the modo form of tuple(poly_index, vert_index).

    Remember that if the layer index, or vmap index, is wrong, you may get
    odd results. This function does not do any form of error checking, and
    modo will often ignore wrong results and choose to give the last
    used "right" results instead of nothing. So, pay attention.

    @param mesh_layer_index: The index of the mesh layer.

    @param vmap_index: The index of the vmap.
    '''
    _layerservice_select('layer.name', str(mesh_layer_index))

    _layerservice_select('vmap.name', str(vmap_index))

    _layerservice_select('uvs', 'selected')

    unformatted_uvs = _layerservice_query('uvs')

    # After a lot of testing, this seems to be the fastest way to handle
    # the conversion in large numbers.
    def convert_uv_strings(uv_string):
        poly_index, vert_index = uv_string[1:-1].split(',')
        return (int(poly_index), int(vert_index))

    if unformatted_uvs is None:
        return None
    else:
        return map(convert_uv_strings, unformatted_uvs)

def get_selected_vertex_indices():
    '''Get the indices of the selected vertices.

    @important: This automatically goes by the user selected mesh item.
    This may not be preferable so options should be supplied in the future.

    @todo: Add more depth to this command, to handle the possibility of
    multiple mesh items being selected. One idea is to return tupple groups
    if verts from multiple mesh items are currently selected.
    eg: ((item_id, (vert_indices)), (item_id, (vert_indices)))
    '''
    _layerservice_select('layer.id', 'selected')

    _layerservice_select('verts', 'selected')
    return moopy.al.utility.convert_strings_to_ints(
        _layerservice_query('verts'))

def get_symmetrical_vertex(vertex_index, mesh_index):
    '''Get what modo considers the symmetrical vertex. Note that for this to
    work at all, some form of symmetry has to be enabled, or this
    will always return None.'''

    _layerservice_select('layer.name', str(mesh_index))

    _layerservice_select('vert.symmetric', str(vertex_index))

    result = _layerservice_query('vert.symmetric')

    if result:
        return int(result)
    else:
        return None

def get_uv_position(uv_map_index, uv_name):
    '''Return the UV Coordinates of a.. uv.

    @param uv_map_index: The.. uv map index.

    @param uv_name: A tuple of what modo considers a uv name, which is
    the layer index of the mesh, the polygon index, and the vertex index.
    '''

    _layerservice_select('vmap.name', str(uv_map_index))

    _layerservice_select('layer.name', str(uv_name[0]))

    _layerservice_select('uv.pos', str((uv_name[1], uv_name[2],)))

    return _layerservice_query('uv.pos')

def get_vertex_position(mesh_index, vertex_index):
    ''''''

    _layerservice_select('layer.name', str(mesh_index))

    _layerservice_select('vert.pos', str(vertex_index))

    return _layerservice_query('vert.pos')

def get_vertex_selected(mesh_index, vertex_index):
    '''Return True if the vertex is selected by the user. False otherwise.
    '''

    _layerservice_select('layer.name', str(mesh_index))

    _layerservice_select('vert.selected', str(vertex_index))

    return not not _layerservice_query('vert.selected')
